<?php
class GoodsClassModel extends MY_Model {

    function __construct()
    {
        parent::__construct();
        $this->_table = 'goods_class';
    }
    
 	function selectById($id) {
    	$result = $this->db->query('select gc.*,gt.name as typename from goods_class gc LEFT JOIN goods_type gt on gc.typeid=gt.id where gc.id='.$id);
    	return $result->row_array();
    }
    
	function selectAll() {
    	$data = $this->db->query('select gc.*,gt.name as typename from goods_class gc LEFT JOIN goods_type gt on gc.typeid=gt.id');
    	return $data->result_array();
    }
    
	function insert($data) {
		
		try {
			$this->db->trans_strict(FALSE);
			$id = parent::insert($data);
			$position = $id;
			if ($data['parentid']) {//parentid != 0
    			$parent = $this->selectById($data['parentid']);
    			$position = $parent['position'].','.$id;
    		}
    		$this->update_position($id, $position);
	    	$this->db->trans_complete();
		} catch (Exception $e) {
			$this->db->trans_rollback();
			return 0;
		}
		return $id;
    }
    
	function update_position($id,$position) {
    	$data['id'] = $id;
    	$data['position'] = $position;
    	$this->db->where('id', $id);
    	return $this->db->update($this->_table,$data);
    }
    
}
?>